<!-- 常用发/收货方 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="activeName == '1' ? '请输入发货人名称' : '请输入收货人名称'"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          :placeholder="activeName == '1' ? '请输入发货人证件号' : '请输入收货人证件号'"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="linkTel">
        <el-input
          v-model="queryParams.linkTel"
          :placeholder="activeName == '1' ? '请输入发货人联系电话' : '请输入收货人联系电话'"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tws:orderAddress:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tws:orderAddress:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tws:orderAddress:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tws:orderAddress:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-tabs class="tabsBox" v-model="activeName" type="card" @tab-click="tabClick">
      <el-tab-pane label="发货地址" name="1"></el-tab-pane>
      <el-tab-pane label="收货地址" name="2"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="orderAddressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="activeName == '1' ? '发货人名称' : '收货人名称'" align="center" prop="name" />
      <el-table-column align="center">
        <span slot="header">
          证件号
          <el-tooltip effect="dark" placement="top" :content="activeName == '1' ? '发货人统一社会信用代码或个人证件号' : '收货人统一社会信用代码或个人证件号'">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </span>
        <template slot-scope="scope">
          <span>{{ scope.row.idCard }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="activeName == '1' ? '发货人联系电话' : '收货人联系电话'" align="center" prop="linkTel" />
      <el-table-column :label="activeName == '1' ? '发货地址' : '收货地址'" align="center" prop="address" show-overflow-tooltip />
      <el-table-column v-if="mode == '2'" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-cehck"
            @click="handleSelect(scope.row)"
          >选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改订单常用发/收货方地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item :label="activeName == '1' ? '发货地址' : '收货地址'" prop="address">
          <el-input v-model="form.address" suffix-icon="el-icon-location-information" placeholder="点击选择地址" @focus="openMap" />
        </el-form-item>
        <el-form-item :label="activeName == '1' ? '发货人名称' : '收货人名称'" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item prop="idCard">
          <span slot="label">
            证件号
            <el-tooltip effect="dark" placement="top" :content="activeName == '1' ? '发货人统一社会信用代码或个人证件号' : '收货人统一社会信用代码或个人证件号'">
              <i class="el-icon-question"></i>
            </el-tooltip>
          </span>
          <el-input v-model="form.idCard" placeholder="请输入证件号" />
        </el-form-item>
        <el-form-item :label="activeName == '1' ? '发货人联系电话' : '收货人联系电话'" prop="linkTel">
          <el-input v-model="form.linkTel" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 地图 -->
    <el-dialog title="选择发货地址" :visible.sync="openMapF" width="1500px" append-to-body :close-on-click-modal="false">
      <GDMapF mapId="orderAddressMapF" @getAddress="getAddress" />
    </el-dialog>

    <!-- 地图 -->
    <el-dialog title="选择收货地址" :visible.sync="openMapS" width="1500px" append-to-body :close-on-click-modal="false">
      <GDMapS mapId="orderAddressMapS" @getAddress="getAddress" />
    </el-dialog>

  </div>
</template>

<script>
import { listOrderAddress, getOrderAddress, delOrderAddress, addOrderAddress, updateOrderAddress } from "@/api/tws/orderAddress";
import GDMapF from '@/components/Map/GDMap';
import GDMapS from '@/components/Map/GDMap';

export default {
  name: "OrderAddress",
  components: {
    GDMapF,
    GDMapS
  },
  props: {
    // 2 可选择
    mode: {
      type: String,
      default: '1'
    },
    // activeName
    active: {
      type: String,
      default: '1'
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 禁用弹窗新增或修改的确定按钮
      disabled: false,
      // 总条数
      total: 0,
      // 订单常用发/收货方地址表格数据
      orderAddressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openMapF: false,
      openMapS: false,
      activeName: '1',
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        type: '1',
        name: null,
        idCard: null,
        linkTel: null,
        address: null,
        cityCode: null,
        longitude: null,
        latitude: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: [ "blur", "change" ] }
        ],
      }
    };
  },
  watch: {
    active: function(newData, oldData) {
      if(newData) {
        this.activeName = newData;
        this.tabClick();
      }
    }
  },
  created() {
    this.tabClick();
  },
  methods: {
    tabClick(name) {
      this.queryParams.current = 1;
      this.queryParams.type = this.activeName;
      this.ids = [];
      this.getList();
    },
    /** 查询订单常用发/收货方地址列表 */
    getList() {
      this.loading = true;
      listOrderAddress(this.queryParams).then(response => {
        this.orderAddressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    openMap() {
      if(this.activeName == '1') {
        this.openMapF = true;
      } else {
        this.openMapS = true;
      }
    },
    // 选择地址的回调
    getAddress(adcode, address, lng, lat) {
      this.openMapF = false;
      this.openMapS = false;
      this.form.cityCode = adcode;
      this.form.address = address;
      this.form.longitude = lng;
      this.form.latitude = lat;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: this.activeName,
        name: null,
        idCard: null,
        linkTel: null,
        address: null,
        cityCode: null,
        longitude: null,
        latitude: null,
        tenantId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 选择了地址
    handleSelect(row) {
      this.$emit("getOrderAddress", row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.activeName == '1' ? '添加常用发货方地址' : '添加常用收货方地址';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderAddress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.activeName == '1' ? '修改常用发货方地址' : '修改常用收货方地址';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.disabled = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.activeName == '1' && !this.form.idCard) {
            this.$modal.msgError("发货人统一社会信用代码或个人证件号不能为空");
            this.disabled = false;
            return
          }
          if (this.form.id != null) {
            updateOrderAddress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.disabled = false;
              this.open = false;
              this.getList();
            }).catch(err => {
              this.disabled = false;
            })
          } else {
            addOrderAddress(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.disabled = false;
              this.open = false;
              this.getList();
            }).catch(err => {
              this.disabled = false;
            })
          }
        } else {
          this.disabled = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除常用发/收货方地址?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrderAddress(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(err => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/orderAddress/export', {
        ...this.queryParams
      }, `tws_orderAddress.xlsx`)
    }
  }
};
</script>

<style>
  .tabsBox {
    margin-top: 20px;
  }
</style>
